package com.miyuan.wm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.miyuan.wm.aop.UserOperation;
import com.miyuan.wm.mapper.GoodsMapper;
import com.miyuan.wm.pojo.GoodsEntity;
import com.miyuan.wm.request.AddGoodsRequestVo;
import com.miyuan.wm.request.QueryGoodsRequestVo;
import com.miyuan.wm.request.UpdateGoodsRequestVo;
import com.miyuan.wm.response.BaseResponse;
import com.miyuan.wm.response.PageResponse;
import com.miyuan.wm.response.QueryGoodByIdResponseVo;
import com.miyuan.wm.response.QueryGoodsResponseVo;
import com.miyuan.wm.service.GoodsService;
import com.miyuan.wm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName GoodsServiceImpl
 * @Description 货物管理接口业务类
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageResponse<List<QueryGoodsResponseVo>> listGoods(QueryGoodsRequestVo requestVo) {
        Page<GoodsEntity> page = new Page<>(requestVo.getPageNumber(), requestVo.getPageSize());
        Wrapper<GoodsEntity> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(requestVo.getName())) {
            wrapper.like("name", requestVo.getName());
        }
        wrapper.orderBy("id", false);
        List<GoodsEntity> goodsEntities = goodsMapper.selectPage(page, wrapper);
        List<QueryGoodsResponseVo> responseVos = BeanUtils.convert(goodsEntities, QueryGoodsResponseVo.class);
        return PageResponse.newInstance(responseVos, page.getTotal(), page.getCurrent(), page.getSize());
    }

    @Override
    @UserOperation("新增货物信息")
    public BaseResponse<Boolean> addGoods(AddGoodsRequestVo requestVo) {
        GoodsEntity entity = BeanUtils.convert(requestVo, GoodsEntity.class);
        goodsMapper.insert(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("修改货物信息")
    public BaseResponse<Boolean> updateGoods(UpdateGoodsRequestVo requestVo) {
        GoodsEntity entity = BeanUtils.convert(requestVo, GoodsEntity.class);
        entity.setId(requestVo.getId());
        goodsMapper.updateById(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("删除货物信息")
    public BaseResponse<Boolean> deleteGoods(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return BaseResponse.errorOf(Boolean.FALSE);
        }
        goodsMapper.deleteBatchIds(ids);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    public BaseResponse<List<QueryGoodsResponseVo>> queryGoodsforSelect() {
        Wrapper<GoodsEntity> wrapper = new EntityWrapper<>();
        List<GoodsEntity> supplierEntities = goodsMapper.selectList(wrapper);
        return BaseResponse.successOf(BeanUtils.convert(supplierEntities, QueryGoodsResponseVo.class));
    }

    @Override
    public BaseResponse<QueryGoodByIdResponseVo> queryGoodById(int id) {
        GoodsEntity goodsEntity = goodsMapper.selectById(id);
        QueryGoodByIdResponseVo goodByIdResponseVo = new QueryGoodByIdResponseVo();
        goodByIdResponseVo.setGoodName(goodsEntity.getName());
        goodByIdResponseVo.setGoodPrice(goodsEntity.getPrice());
        goodByIdResponseVo.setGoodSize(goodsEntity.getSize());
        goodByIdResponseVo.setGoodType(goodsEntity.getType());
        return BaseResponse.successOf(goodByIdResponseVo);
    }

}
